package org.irri.smta.global;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.irri.smta.domain.AccessPrivilege;
import org.irri.smta.domain.Action;
import org.irri.smta.domain.Role;
import org.irri.smta.domain.User;
import org.irri.smta.service.IAccessPrivilegeService;
import org.irri.smta.service.IRoleService;
import org.irri.smta.service.IUserService;
import org.irri.smta.util.BeanUtils;
import org.springframework.stereotype.Component;

@Component("globalBean")
@ApplicationScoped
public class GlobalBean implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private List<AccessPrivilege> accessPrivilegeList = new ArrayList<AccessPrivilege>();
	private List<Role> roleList = new ArrayList<Role>();
	private List<User> userList = new ArrayList<User>();
	private BeanUtils beanUtils = new BeanUtils();
	
	@Inject
	@Named("accessPrivilegeService")
	private IAccessPrivilegeService<AccessPrivilege, Long> accessPrivilegeService;
	
	@Inject
	@Named("roleService")
	private IRoleService<Role, Long> roleService;

	@Inject
	@Named("userService")
	private IUserService<User, Long> userService;
	
	public GlobalBean() {
		super();
	}
	
	@PostConstruct
	public void init() {
		load();
	}
	
	public void load() {
		loadAccessPrivilegeList();
		loadRoleList();
		loadUserList();
	}

	public List<AccessPrivilege> getAccessPrivilegeList() {
		return accessPrivilegeList;
	}

	public void setAccessPrivilegeList(List<AccessPrivilege> accessPrivilegeList) {
		this.accessPrivilegeList = accessPrivilegeList;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	
	//////////
	public void loadAccessPrivilegeList() {
		accessPrivilegeList = accessPrivilegeService.findAllOrdered();
	}

	public void loadRoleList() {
		roleList = roleService.findAllOrdered();
	}

	public void loadUserList() {
		userList = userService.findAllOrdered();
	}
	
	public Boolean canCreate() {
		return canCreate(beanUtils.getSessionUser());
	}
	
	public Boolean canCreate(User theUser) {
		if (theUser != null) {
			for (AccessPrivilege ap : accessPrivilegeList) {
				if (ap.getRole().getId().longValue() == theUser.getRole().getId().longValue()) {
					if (ap.getAction().equalsIgnoreCase(Action.CREATE.getCode())) {
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public Boolean canRead() {
		return canRead(beanUtils.getSessionUser());
	}

	public Boolean canRead(User theUser) {
		if (theUser != null) {
			for (AccessPrivilege ap : accessPrivilegeList) {
				if (ap.getRole().getId().longValue() == theUser.getRole().getId().longValue()) {
					if (ap.getAction().equalsIgnoreCase(Action.READ.getCode())) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public Boolean canUpdate() {
		return canUpdate(beanUtils.getSessionUser());
	}

	public Boolean canUpdate(User theUser) {
		if (theUser != null) {
			for (AccessPrivilege ap : accessPrivilegeList) {
				if (ap.getRole().getId().longValue() == theUser.getRole().getId().longValue()) {
					if (ap.getAction().equalsIgnoreCase(Action.UPDATE.getCode())) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public Boolean canDelete() {
		return canDelete(beanUtils.getSessionUser());
	}

	public Boolean canDelete(User theUser) {
		if (theUser != null) {
			for (AccessPrivilege ap : accessPrivilegeList) {
				if (ap.getRole().getId().longValue() == theUser.getRole().getId().longValue()) {
					if (ap.getAction().equalsIgnoreCase(Action.DELETE.getCode())) {
						return true;
					}
				}
			}
		}
		return false;
	}

}
